package netbayServlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import util.Json.JsonUtil;
import util.String.StringUtil;
import util.jQgrid.JSONUtil;

import netbay.NetbayUtility;
import netbay.TransactionManager;
import netbay.UserManager;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
    }
    
	public void init() {
		String configPath = getServletContext().getRealPath(
				"/WEB-INF/config.properties");
		NetbayUtility.init(configPath);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");  // ensures that user input is interpreted as // 8-bit Unicode (e.g., for Czech characters)
        response.setContentType("text/json;charset=utf-8");
        String  url = "/Login.jsp";
        request.getRequestDispatcher(url).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = response.getWriter();
			HttpSession session = request.getSession();
			UserManager um = new UserManager();
			String strJson = "{}";
			String username = (!StringUtil.isNullOrEmpty(request.getParameter("username"))) ? request.getParameter("username").trim() : "";
			String password = (!StringUtil.isNullOrEmpty(request.getParameter("password"))) ? request.getParameter("password").trim() : "";
			try{
				List result = null;
				String hashPassword = UserManager.hashPassword(password);
				result = um.getLoginVerify(username,hashPassword,password);
				if(!(result.isEmpty())){
		        	strJson = "{\"success\": \"\"}";
		        	session.setAttribute("userId",result.get(0));
		        	session.setAttribute("group",result.get(1));
		        	session.setAttribute("tcdTracking",result.get(2));
		        	session.setAttribute("rdTracking",result.get(3));
		        	session.setAttribute("tcdBilling",result.get(4));
		        	session.setAttribute("rdBilling",result.get(5));
		        	session.setAttribute("manageUsers",result.get(6));
		        	session.setAttribute("manageGroups",result.get(7));
		        	session.setAttribute("userLog",result.get(8));
		        	session.setAttribute("reTCDTCD",result.get(9));
		        	session.setAttribute("reJPMCTCD",result.get(10));
		        	session.setAttribute("reRDRD",result.get(11));
		        	session.setAttribute("reJPMCRD",result.get(12));
		        	um.saveLog((String)result.get(0),um.actionLogin,"");
		        }
				else {
		        	strJson = "{\"not\": \"\"}";
		        	session.setAttribute("userId","null");
		        	session.setAttribute("group","null");
		        	session.setAttribute("tcdTracking","null");
		        	session.setAttribute("rdTracking","null");
		        	session.setAttribute("tcdBilling","null");
		        	session.setAttribute("rdBilling","null");
		        	session.setAttribute("manageUsers","null");
		        	session.setAttribute("manageGroups","null");
		        	session.setAttribute("userLog","null");
		        	session.setAttribute("reTCDTCD","null");
		        	session.setAttribute("reJPMCTCD","null");
		        	session.setAttribute("reRDRD","null");
		        	session.setAttribute("reJPMCRD","null");
		        }
		}catch (Exception ex) {
			strJson = "{\"error\": \"" + ex.getMessage() + "\"}" ;
		}
        out.write(strJson);
	}

	
	
}
